package com.party.web.biz.member;

import com.party.core.model.YesNoStatus;
import com.party.core.model.activity.Activity;
import com.party.core.model.article.Article;
import com.party.core.model.commune.CMoment;
import com.party.core.model.commune.MomentType;
import com.party.core.model.dynamic.Dynamic;
import com.party.core.model.member.MemberGroup;
import com.party.core.service.commune.ICMomentService;
import com.party.core.service.cos.CosBizService;
import com.party.core.service.dynamic.IDypicsService;
import com.party.core.service.dynamic.biz.DynamicBaseBizService;
import com.party.core.service.member.IMemberGroupService;
import com.party.core.service.member.IMemberService;
import com.party.core.utils.MyBeanUtils;
import com.party.web.utils.RealmUtils;
import com.party.web.web.dto.input.member.CMomentInput;
import com.party.web.web.dto.output.member.CMomentSave;
import com.party.web.web.security.CurrentUser;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;

/**
 * @program: party
 * @description: 动态业务
 * @author: Hua Qunhao
 * @create: 2019-12-18 18:57
 **/
@Service
public class CMomentBizService {
    @Autowired
    private ICMomentService momentService;
    @Autowired
    private IDypicsService dypicsService;
    @Autowired
    private DynamicBaseBizService dynamicBaseBizService;
    @Autowired
    private CosBizService cosBizService;
    @Autowired
    private IMemberService memberService;
    @Autowired
    private IMemberGroupService memberGroupService;
    @Autowired
    private CIntegralBizToService cIntegralBizToService;

    Logger logger = LoggerFactory.getLogger(getClass());

    public String saveBizCMoment(CMomentInput cMoment) throws Exception {
        CurrentUser currentUser = RealmUtils.getNewCurrentUser();
        String memberId = currentUser.getId();
        if(currentUser.isXzgsAdmin()){
            memberId = currentUser.getPartnerId();
        }
        if (StringUtils.isNotEmpty(cMoment.getCreateBy())) {
            memberId = cMoment.getCreateBy();
        }
        if (StringUtils.isNotEmpty(cMoment.getId())) {//编辑表单保存
            CMoment dy = momentService.get(cMoment.getId());//从数据库取出记录的值
            dy.setUpdateBy(currentUser.getId());
            cMoment.setCreateBy(dy.getCreateBy());
            if (MomentType.MEMBER_JOIN.getCode() == cMoment.getMomentType()) {
                dy.setBusinessIds(cMoment.getMembers());
            }

            MyBeanUtils.copyBeanNotNull2Bean(cMoment, dy);//将编辑表单中的非\NULL值覆盖数据库记录中的值
            momentService.update(dy);//保存
            return cMoment.getId();
        } else {//新增表单保存
            cMoment.setUpdateBy(memberId);
            cMoment.setCreateBy(memberId);

            if (MomentType.MEMBER_JOIN.getCode() == cMoment.getMomentType()) {
                cMoment.setBusinessIds(cMoment.getMembers());
            }
            //发送方式
            cMoment.setMomentWay(YesNoStatus.YES.getCode());
            return momentService.insert(cMoment);
        }
    }

    /**
     * 删除动态
     *
     * @param id 动态编号
     * @return
     */
    public boolean delBiz(String id) {
        boolean endResult = true;
        try {
            if (StringUtils.isEmpty(id)) {
                endResult = false;
                return endResult;
            }
            CMoment moment = momentService.get(id);
            if (moment == null) {
                endResult = false;
                return endResult;
            }
            // 删除动态图片
            dypicsService.deleteByRefIdLogic(id);
            // 删除动态
            moment.setDelFlag(CMoment.DEL_FLAG_DELETE);
            momentService.update(moment);
            return true;
        } catch (Exception e) {
            endResult = false;
            logger.error("删除动态异常", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return endResult;
    }
    /**
     * @Description:
     * @Param:
     * @return:
     * @Author: Hua Qunhao
     * @Date: 2019/12/22
     */
    public String businessSaveCMoment(CMomentSave save) throws Exception {

        MemberGroup memberGroup = memberGroupService.get(save.getPartnerId());
        //是否拥有行知公社权限
        boolean isCommune = cIntegralBizToService.isXZGSJurisdiction(memberGroup.getId());
        if(!isCommune){
            return null;
        }
        CMoment queryMoment = new CMoment();
        queryMoment.setTargetId(save.getBizId());
        queryMoment.setMomentType(save.getMomentType());
        List<CMoment> moments = momentService.list(queryMoment);
        if (moments.size()>0) {
            return null;
        }
        CMoment cMoment = new CMoment();
        if (MomentType.ACTIVITY_PUBLISH.getCode() == save.getMomentType()) {
            Activity activity = save.getActivity();
            cMoment = new CMoment();
            cMoment.setTargetId(activity.getId());
            cMoment.setMomentType(MomentType.ACTIVITY_PUBLISH.getCode());
            cMoment.setPics(activity.getPic());
            cMoment.setCreateBy(activity.getMember());
            cMoment.setContent(activity.getTitle());
        } else if (MomentType.TOPIC_PUBLISH.getCode() == save.getMomentType()) {
            Dynamic dynamic = save.getDynamic();
            cMoment = new CMoment();
            cMoment.setTargetId(dynamic.getId());
            cMoment.setMomentType(MomentType.TOPIC_PUBLISH.getCode());
            cMoment.setPics(dynamic.getPics());
            cMoment.setCreateBy(memberGroup.getId());
            cMoment.setMemberId(dynamic.getCreateBy());
            cMoment.setContent(dynamic.getContent());
        } else if (MomentType.PHOTO_PUBLISH.getCode() == save.getMomentType()) {
            Map<String, Object> mmp = save.getDatas();
            cMoment = new CMoment();
            cMoment.setTargetId(mmp.get("photoProjectId").toString());
            cMoment.setMomentType(MomentType.PHOTO_PUBLISH.getCode());
            cMoment.setPics(mmp.get("pics").toString());
            cMoment.setCreateBy(mmp.get("createBy").toString());
            cMoment.setContent(mmp.get("photoTitle").toString());
        } else if (MomentType.ADD_INTEGRAL.getCode() == save.getMomentType()) {
            Map<String, Object> mmp = save.getDatas();
            cMoment = new CMoment();
            cMoment.setTargetId(mmp.get("activityId").toString());
            cMoment.setMomentType(MomentType.ADD_INTEGRAL.getCode());
            cMoment.setCreateBy(mmp.get("createBy").toString());
            cMoment.setContent("恭喜下列小伙伴参加活动获得积分" + mmp.get("itemFraction").toString() + "分");
            cMoment.setBusinessIds(mmp.get("memberIds").toString());
        } else if (MomentType.NEWS.getCode() == save.getMomentType()) {
            Article article = save.getArticle();
            cMoment = new CMoment();
            cMoment.setTargetId(article.getId());
            cMoment.setMomentType(MomentType.NEWS.getCode());
            cMoment.setCreateBy(article.getMemberId());
            cMoment.setContent(article.getTitle());
            cMoment.setPics(article.getPic());
        }
        cMoment.setMomentWay(YesNoStatus.YES.getCode());
        return momentService.insert(cMoment);
    }
}
